package com.xiaoxing.gongqiju.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gongqiju.R;
import com.xiaoxing.gongqiju.R2;
import com.xiaoxing.gongqiju.di.component.DaggerLiaoCheng_GongQiJu_YingJiChuKuComponent;
import com.xiaoxing.gongqiju.di.module.LiaoCheng_GongQiJu_YingJiChuKuModule;
import com.xiaoxing.gongqiju.mvp.contract.LiaoCheng_GongQiJu_YingJiChuKuContract;
import com.xiaoxing.gongqiju.mvp.presenter.LiaoCheng_GongQiJu_YingJiChuKuPresenter;
import com.xiaoxing.gongqiju.mvp.ui.entity.LiaoCheng_GongQiJu_YingJiChuKu;
import com.xw.repo.XEditText;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.GONGQIJU_LIAOCHENG_ACTIVITY_LIAO_CHENG_GONG_QI_JU_YING_JI_CHU_KU)
public class LiaoCheng_GongQiJu_YingJiChuKuActivity extends BaseActivity<LiaoCheng_GongQiJu_YingJiChuKuPresenter> implements LiaoCheng_GongQiJu_YingJiChuKuContract.View {


    @BindView(R2.id.tv_yi_sao_miao_er_wei_ma)
    TextView tvYiSaoMiaoErWeiMa;
    @BindView(R2.id.xet_chu_ku_shu_liang)
    XEditText xetChuKuShuLiang;
    private String scanResult = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLiaoCheng_GongQiJu_YingJiChuKuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .liaoCheng_GongQiJu_YingJiChuKuModule(new LiaoCheng_GongQiJu_YingJiChuKuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_liao_cheng__gong_qi_ju__ying_ji_chu_ku; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    private void toScanQRCodeActivity() {
        Intent openCameraIntent = new Intent(this, LiaoCheng_GongQiJu_ScanActivity.class);
        mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, 101);
        startActivityForResult(openCameraIntent, 101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 101) {
            Bundle bundle = data.getExtras();
            scanResult = bundle.getString("scan_result");
            tvYiSaoMiaoErWeiMa.setText("已扫描的二维码：" + scanResult);
        }

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_liao_cheng__gong_qi_ju__ying_ji_chu_ku));
        //getLiaoCheng_GongQiJu_YingJiChuKuData();
    }

    @Override
    public void getLiaoCheng_GongQiJu_YingJiChuKuDataSuccess(LiaoCheng_GongQiJu_YingJiChuKu entityData) {

        showMessage(entityData.getMessage());
        if (entityData.getCode().equals("200")) {
            scanResult = "";
            xetChuKuShuLiang.setText("");
        }

    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R2.id.btn_bao_cun)
    public void onViewClicked() {
        if (TextUtils.isEmpty(scanResult)) {
            showMessage("请先扫描物料二维码");
            toScanQRCodeActivity();
            return;
        }
        if (TextUtils.isEmpty(getChuKuShuLiang())) {
            showMessage("请输入出库数量");
            return;
        }
        String jsonData = "[\n" +
                "  {\n" +
                "    \"oid\": 0,\n" +
                "    \"opid\": 0,\n" +
                "    \"mid\": " + scanResult + ",\n" +
                "    \"spid\": 0,\n" +
                "    \"OutNum\": " + getChuKuShuLiang() + ",\n" +
                "    \"outtime\": \"" + "" + "\",\n" +
                "    \"outuserid\": " + mSharedPreferencesHelper.getString(BaseConstants.UID) + ",\n" +
                "    \"outstatus\": 1\n" +
                "  }\n" +
                "]";
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonData);
        mPresenter.getLiaoCheng_GongQiJu_YingJiChuKuData(body);
    }

    @NotNull
    private String getChuKuShuLiang() {
        return xetChuKuShuLiang.getText().toString().trim();
    }


    @OnClick(R2.id.img_er_wei_ma)
    public void imgErWeiMa() {
        toScanQRCodeActivity();
    }
}
